/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Formulario_login.java
 *
 * Created on 18/06/2011, 10:31:31
 */
package formularios;

import formularios.model.Empregados;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author juliana
 */
public class Formulario_login extends javax.swing.JFrame {

    /** Creates new form Formulario_login */
    public Formulario_login() {
        initComponents();
        msg_invalido.setVisible(false);
        txt_emp_senha.setText("");
        getRootPane().setDefaultButton(btn_login);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        try {
            MaskFormatter formato_cpf = new MaskFormatter("###.###.###-##");
            txt_emp_cpf = new JFormattedTextField(formato_cpf);
            lab_emp_cpf = new javax.swing.JLabel();
            txt_emp_senha = new javax.swing.JPasswordField();
            btn_login = new javax.swing.JButton();
            msg_invalido = new javax.swing.JLabel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            jLabel1.setText("Senha");

        } catch (ParseException ex) {
            Logger.getLogger(Formulario_cliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        txt_emp_cpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txt_emp_cpfFocusLost(evt);
            }
        });

        lab_emp_cpf.setText("CPF");

        txt_emp_senha.setText("jPasswordField1");

        btn_login.setText("Login");
        btn_login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_loginActionPerformed(evt);
            }
        });

        msg_invalido.setText("Usuário ou senha inexistente.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(lab_emp_cpf))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_login)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(msg_invalido))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txt_emp_cpf, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txt_emp_senha, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_emp_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lab_emp_cpf))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_emp_senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_login)
                    .addComponent(msg_invalido))
                .addContainerGap(78, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_emp_cpfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_emp_cpfFocusLost
}//GEN-LAST:event_txt_emp_cpfFocusLost

    private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_loginActionPerformed
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BancoImobiliaria");
        EntityManager em = emf.createEntityManager();

        //procura o empregado com o CPF digitado
        Empregados emp = em.find(Empregados.class, txt_emp_cpf.getText());
        JFrame_principal formPrincipal = null;

        boolean loginOk = false;

        if (emp != null) {
            char[] senha_digitada = txt_emp_senha.getPassword();
            String senha_digitada_str;
            if (senha_digitada == null) {
                senha_digitada_str = "";
            } else {
                senha_digitada_str = String.valueOf(senha_digitada);
            }
            if (emp.getSenha() == senha_digitada_str.hashCode()) {
                formPrincipal = new JFrame_principal();
                formPrincipal.login(emp.getCpf(), emp.getCargo());
                formPrincipal.setVisible(true);
                loginOk = true;
            }
        }
        if (loginOk) {
            this.setVisible(false);
        } else {
            msg_invalido.setVisible(false);
            txt_emp_senha.setText("");
            txt_emp_senha.requestFocus();
        }
    }//GEN-LAST:event_btn_loginActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_login;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lab_emp_cpf;
    private javax.swing.JLabel msg_invalido;
    private javax.swing.JTextField txt_emp_cpf;
    private javax.swing.JPasswordField txt_emp_senha;
    // End of variables declaration//GEN-END:variables
}
